﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.DAL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MAS_ExpenseRates : System.Web.UI.Page
    {
        Main main = new Main();
        //IBLL_FIM_MAS_ExpenseRateTypeGroupMappings RateTypeGrpMap = new B_FIM_MAS_ExpenseRateTypeGroupMappings();
        //IBLL_FIM_MAS_ExpenseRateTypeGroups RateTypeGrp = new B_FIM_MAS_ExpenseRateTypeGroups();
        //IBLL_FIM_MAS_ExpenseRateTypes RateType = new B_FIM_MAS_ExpenseRateTypes();
        IBLL_FIM_MAS_ExpenseRates Rate = new B_FIM_MAS_ExpenseRates();
        //IBLL_FIM_MAS_ExpenseRateMappings RateMap = new B_FIM_MAS_ExpenseRateMappings();
        IBLL_BGM_MAS_ExpenseGroup ExpGrp = new B_BGM_MAS_ExpenseGroup();
        IBLL_BGM_MAS_Expense Exp = new B_BGM_MAS_Expense();
        IBLL_BGM_MAS_Source Source = new B_BGM_MAS_Source();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //======= test only =========
                //Session["EmpSeq"] = "1";
                //===========================

                UpdateUI2("ViewAll");
                //LoadRadGrid2("-1");
                RadGrid2.Enabled = false;
                PanelControl2.Enabled = false;
                LoadDDLBgmSourceSeq();
                LoadRoot();
            }
        }

        protected void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = Source.GetList();
            ddlBgmSourceSeq.DataBind();

            ddlBgmSourceSeq.SelectedIndex = 0;
        }

        protected void LoadRoot()
        {
            RadTreeView1.Nodes.Clear();

            IList<M_BGM_MAS_ExpenseGroup> infolist = ExpGrp.GetList(null);

            foreach (M_BGM_MAS_ExpenseGroup info in infolist)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.bgm_exp_grp_name;
                node.Value = info.bgm_exp_grp_seq.ToString();
                node.Category = "grp";
                node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                RadTreeView1.Nodes.Add(node);
            }

            LoadRadGrid2("-1");
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            IList<M_BGM_MAS_Expense> infolist;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;

            if (e.Node.Nodes.Count == 0)
            {
                if (e.Node.Category == "grp")
                {
                    infolist = Exp.GetGroupChildList(e.Node.Value, strBgmSourceSeq);
                }
                else
                {
                    infolist = Exp.GetChildList(e.Node.Value, strBgmSourceSeq);
                }

                foreach (M_BGM_MAS_Expense info in infolist)
                {
                    RadTreeNode node = new RadTreeNode();
                    node.Text = info.bgm_exp_name;
                    node.Value = info.bgm_exp_seq.ToString();

                    if (info.child_count > 0)
                    {
                        node.Category = "exp";
                        node.ExpandMode = expandMode;
                    }
                    else
                    {
                        node.Category = "leaf";
                    }

                    e.Node.Nodes.Add(node);
                }
            }

            e.Node.Expanded = true;
        }

        protected void LoadRadGrid2(string strBgmExpSeq)
        {
            RadGrid2.DataSource = Rate.GetFull(strBgmExpSeq, ddlBgmSourceSeq.SelectedValue);
            RadGrid2.DataBind();
        }

        //protected void LoadDataRepeaterDtls()
        //{
        //    string strBgmExpSeq = lblBgmExpSeq.Text;

        //    RepeaterDtls.DataSource = RateTypeGrp.GetFull(strBgmExpSeq);
        //    RepeaterDtls.DataBind();
        //}

        protected void RepeaterDtls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item is RepeaterItem)
            {
                //if (e.Item.ItemType != ListItemType.Header)
                //{
                //    RepeaterItem dataItem = (RepeaterItem)e.Item;
                //    Label lbl = (Label)dataItem.FindControl("lblExpRateTypeGrpSeq");
                //    //Label lbl2 = (Label)dataItem.FindControl("lblExpRateMapSeq");
                //    RadComboBox ddl = (RadComboBox)dataItem.FindControl("ddlExpRateTypeSeq");

                //    ddl.DataSource = RateType.GetList(lbl.Text);
                //    ddl.DataBind();

                //    main.InsertEmptyDDLAll(ddl);
                //    ddl.SelectedIndex = 0;
                //}
            }
        }

        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll2.Visible = false;
                    cmdAddNew2.Visible = true;
                    cmdEdit2.Visible = true;
                    cmdDelete2.Visible = true;
                    cmdInsert2.Visible = false;
                    cmdUpdate2.Visible = false;

                    PanelRadGrid2.Visible = true;
                    PanelEdit2.Visible = false;
                    PanelControl1.Enabled = true;
                    PanelRadGrid1.Enabled = true;
                    break;
                case "AddNew":
                    cmdViewAll2.Visible = true;
                    cmdAddNew2.Visible = false;
                    cmdEdit2.Visible = false;
                    cmdDelete2.Visible = false;
                    cmdInsert2.Visible = true;
                    cmdUpdate2.Visible = false;

                    PanelRadGrid2.Visible = false;
                    PanelEdit2.Visible = true;
                    PanelControl1.Enabled = false;
                    PanelRadGrid1.Enabled = false;
                    break;
                case "Edit":
                    cmdViewAll2.Visible = true;
                    cmdAddNew2.Visible = false;
                    cmdEdit2.Visible = false;
                    cmdDelete2.Visible = false;
                    cmdInsert2.Visible = false;
                    cmdUpdate2.Visible = true;

                    PanelRadGrid2.Visible = false;
                    PanelEdit2.Visible = true;
                    PanelControl1.Enabled = false;
                    PanelRadGrid1.Enabled = false;
                    break;
            }
        }

        private bool CheckData()
        {
            string strExpRateName = txtExpRateName.Text;
            //string strAmountMin = txtAmountMin.Text;
            string strAmountMax = txtAmountMax.Text;
            bool bOK = true;

            if (strExpRateName.Length == 0)
            {
                bOK = false;
                imgExpRateName.Visible = true;
            }
            //else if (strAmountMin.Length == 0)
            //{
            //    bOK = false;
            //    imgAmountMin.Visible = true;
            //}
            else if (strAmountMax.Length == 0)
            {
                bOK = false;
                imgAmountMax.Visible = true;
            }
            else
            {
                imgExpRateName.Visible = false;
                //imgAmountMin.Visible = false;
                imgAmountMax.Visible = false;
            }

            return bOK;
        }

        protected void ClearControl2()
        {
            lblExpRateSeq.Text = "";
            txtExpRateName.Text = "";
            txtExpRateDescr.Text = "";
            //txtAmountMin.Value = 0;
            txtAmountMax.Value = 0;
        }

        //============================ control events ==============================

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter")// || e.CommandName == "Filter")
            {
                LoadRadGrid2(RadTreeView1.SelectedValue);
            }
        }

        protected void cmdViewAll2_Click(object sender, EventArgs e)
        {
            UpdateUI2("ViewAll");

            ClearControl2();
        }

        protected void cmdAddNew2_Click(object sender, EventArgs e)
        {
            UpdateUI2("AddNew");

            string strBgmExpSeq = RadTreeView1.SelectedValue;
            string strBgmExpName = RadTreeView1.SelectedNode.Text;

            lblBgmExpSeq.Text = strBgmExpSeq;
            lblBgmExpName.Text = strBgmExpName;
        }

        protected void cmdEdit2_Click(object sender, EventArgs e)
        {
            if (RadGrid2.SelectedItems.Count > 0)
            {
                UpdateUI2("Edit");

                string strExpRateSeq = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["exp_rate_seq"].Text;
                string strExpRateName = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["exp_rate_name"].Text;
                string strExpRateDescr = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["exp_rate_descr"].Text;
                //string strAmountMin = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["amount_min"].Text;
                string strAmountMax = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["amount_max"].Text;
                string strBgmExpSeq = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["bgm_exp_seq"].Text;
                string strRelateMan = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["relate_man"].Text;
                string strRelateDay = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["relate_day"].Text;
                string strBgmExpName = RadTreeView1.SelectedNode.Text;

                lblBgmExpSeq.Text = main.WebString(strBgmExpSeq);
                lblBgmExpName.Text = main.WebString(strBgmExpName);
                lblExpRateSeq.Text = main.WebString(strExpRateSeq);
                //txtAmountMin.Text = main.WebString(strAmountMin);
                txtAmountMax.Text = main.WebString(strAmountMax);
                txtExpRateName.Text = main.WebString(strExpRateName);
                txtExpRateDescr.Text = main.WebString(strExpRateDescr);

                chkRelateMan.Checked = strRelateMan == "True";
                chkRelateDay.Checked = strRelateDay == "True";

                //for (int i = 0; i < RepeaterDtls.Items.Count; i++)
                //{
                //    Label lbl = (Label)RepeaterDtls.Items[i].FindControl("lblExpRateMapSeq");
                //    Label lbl2 = (Label)RepeaterDtls.Items[i].FindControl("lblExpRateTypeGrpSeq");
                //    RadComboBox ddl = (RadComboBox)RepeaterDtls.Items[i].FindControl("ddlExpRateTypeSeq");

                //    M_FIM_MAS_ExpenseRateMappings info = RateMap.GetInfo(strExpRateSeq, lbl2.Text);

                //    string strExpRateMapSeq = info.exp_rate_map_seq.ToString();
                //    string strExpRateTypeSeq = info.exp_rate_type_seq.ToString();

                //    lbl.Text = strExpRateMapSeq;
                //    ddl.SelectedValue = strExpRateTypeSeq;
                //}
            }
            else
            {
            }
        }

        protected void cmdDelete2_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_ExpenseRates info = new M_FIM_MAS_ExpenseRates();
            string strPK = RadGrid2.Items[RadGrid2.SelectedItems[0].ItemIndex]["exp_rate_seq"].Text;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.exp_rate_seq = Convert.ToInt32(strPK);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            Rate.Delete(info);
            LoadRadGrid2(RadTreeView1.SelectedValue);
        }

        protected void cmdInsert2_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_ExpenseRates info = new M_FIM_MAS_ExpenseRates();

            string strExpRateSeq;
            string strExpRateName = txtExpRateName.Text;
            string strExpRateDescr = txtExpRateDescr.Text;
            //string strAmountMin = txtAmountMin.Text;
            string strAmountMax = txtAmountMax.Text;
            string strBgmExpSeq = lblBgmExpSeq.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strRelateMan = chkRelateMan.Checked ? "1" : "0";
            string strRelateDay = chkRelateDay.Checked ? "1" : "0";
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            if (!CheckData())
            {
                return;
            }

            info.exp_rate_name = strExpRateName;
            info.exp_rate_descr = strExpRateDescr;
            info.amount_min = 0;
            info.amount_max = main.ConvertNullDouble(strAmountMax);
            info.bgm_exp_seq = main.ConvertNullInt(strBgmExpSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.relate_man = strRelateMan;
            info.relate_day = strRelateDay;

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);

            strExpRateSeq = Rate.Insert(info);

            //for (int i = 0; i < RepeaterDtls.Items.Count; i++)
            //{
            //    RadComboBox ddl = (RadComboBox)RepeaterDtls.Items[i].FindControl("ddlExpRateTypeSeq");
            //    M_FIM_MAS_ExpenseRateMappings infoMap = new M_FIM_MAS_ExpenseRateMappings();
            //    infoMap.exp_rate_seq = main.ConvertNullInt(strExpRateSeq);
            //    infoMap.exp_rate_type_seq = main.ConvertNullInt(ddl.SelectedValue);
            //    infoMap.created_by = main.ConvertNullInt(strUserID);
            //    infoMap.created_when = main.ConvertNullDateTime(dtNow);

            //    RateMap.Insert(infoMap);
            //}

            LoadRadGrid2(strBgmExpSeq);
            UpdateUI2("Insert");
            ClearControl2();
        }

        protected void cmdUpdate2_Click(object sender, EventArgs e)
        {
            M_FIM_MAS_ExpenseRates info = new M_FIM_MAS_ExpenseRates();

            string strExpRateSeq = lblExpRateSeq.Text;
            string strExpRateName = txtExpRateName.Text;
            string strExpRateDescr = txtExpRateDescr.Text;
            //string strAmountMin = txtAmountMin.Text;
            string strAmountMax = txtAmountMax.Text;
            string strBgmExpSeq = lblBgmExpSeq.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strRelateMan = chkRelateMan.Checked ? "1" : "0";
            string strRelateDay = chkRelateDay.Checked ? "1" : "0";
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            if (!CheckData())
            {
                return;
            }

            info.exp_rate_seq = Convert.ToInt32(strExpRateSeq);
            info.exp_rate_name = strExpRateName;
            info.exp_rate_descr = strExpRateDescr;
            info.amount_min = 0;
            info.amount_max = main.ConvertNullDouble(strAmountMax);
            info.bgm_exp_seq = main.ConvertNullInt(strBgmExpSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.relate_man = strRelateMan;
            info.relate_day = strRelateDay;

            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            Rate.Update(info);

            //for (int i = 0; i < RepeaterDtls.Items.Count; i++)
            //{
            //    Label lbl = (Label)RepeaterDtls.Items[i].FindControl("lblExpRateMapSeq");
            //    RadComboBox ddl = (RadComboBox)RepeaterDtls.Items[i].FindControl("ddlExpRateTypeSeq");
            //    M_FIM_MAS_ExpenseRateMappings infoMap = new M_FIM_MAS_ExpenseRateMappings();

            //    infoMap.exp_rate_seq = main.ConvertNullInt(strExpRateSeq);
            //    infoMap.exp_rate_type_seq = main.ConvertNullInt(ddl.SelectedValue);

            //    if (lbl.Text.Length > 0)
            //    {
            //        infoMap.exp_rate_map_seq = Convert.ToInt32(lbl.Text);
            //        infoMap.updated_by = main.ConvertNullInt(strUserID);
            //        infoMap.updated_when = main.ConvertNullDateTime(dtNow);

            //        RateMap.Update(infoMap);
            //    }
            //    else
            //    {
            //        infoMap.created_by = main.ConvertNullInt(strUserID);
            //        infoMap.created_when = main.ConvertNullDateTime(dtNow);

            //        RateMap.Insert(infoMap);
            //    }                
            //}

            LoadRadGrid2(strBgmExpSeq);
            UpdateUI2("Update");
            ClearControl2();
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {

        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            if (e.Node.Category == "leaf")
            {
                e.Node.Selected = true;
                string strBgmExpSeq = e.Node.Value;
                RadGrid2.Enabled = true;
                PanelControl2.Enabled = true;
                LoadRadGrid2(strBgmExpSeq);
                lblBgmExpSeq.Text = strBgmExpSeq;

                //LoadDataRepeaterDtls();
            }
            else
            {
                e.Node.Toggle();
            }
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);
        }

        protected void ddlBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRoot();
        }

        protected void cmdExpandAll_Click(object sender, ImageClickEventArgs e)
        {
            foreach (RadTreeNode node in RadTreeView1.Nodes)
            {
                ExpandAll(node);
                node.ExpandChildNodes();
            }
        }

        protected RadTreeNode ExpandAll(RadTreeNode node)
        {
            string strCategory = node.Category;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            if (strCategory != "leaf")
            {
                if (node.Nodes.Count == 0)
                {
                    IList<M_BGM_MAS_Expense> infolist;
                    if (strCategory == "grp")
                    {
                        infolist = Exp.GetGroupChildList(node.Value, strBgmSourceSeq);
                    }
                    else
                    {
                        infolist = Exp.GetChildList(node.Value, strBgmSourceSeq);
                    }

                    foreach (M_BGM_MAS_Expense info in infolist)
                    {
                        RadTreeNode newNode = new RadTreeNode();
                        newNode.Text = info.bgm_exp_name;
                        newNode.Value = info.bgm_exp_seq.ToString();

                        if (info.child_count > 0)
                        {
                            newNode.Category = "exp";
                            newNode.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                        }
                        else
                        {
                            newNode.Category = "leaf";
                        }

                        newNode = ExpandAll(newNode);
                        node.Nodes.Add(newNode);
                    }
                }

                node.Expanded = true;
            }

            return node;
        }

        protected void cmdCollapseAll_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadGrid2_PageIndexChanged(object source, GridPageChangedEventArgs e)
        {
            LoadRadGrid2(RadTreeView1.SelectedValue);
        }

        //==========================================================================
    }
}
